#include "rtda/heap/method_lookup.h"
#include <vector>
#include <memory>

namespace jvm {
    namespace heap {

        std::shared_ptr<Method> lookupMethodInClass(std::shared_ptr<Class> pClass,
            const std::string& name,
            const std::string& descriptor) 
        {
            for (auto c = pClass; c != nullptr; c = c->getSuperClass()) {
                for (auto method : c->getMethods()) {
                    if (method->getName() == name && method->getDescriptor() == descriptor) {
                        return method;
                    }
                }
            }
            return nullptr;
        }

        std::shared_ptr<Method> lookupMethodInInterfaces(const std::vector<std::shared_ptr<Class>>& interfaces,
            const std::string& name,
            const std::string& descriptor) {
            for (auto iface : interfaces) {
                for (auto method : iface->getMethods()) {
                    if (method->getName() == name && method->getDescriptor() == descriptor) {
                        return method;
                    }
                }

                auto method = lookupMethodInInterfaces(iface->getInterfaces(), name, descriptor);
                if (method != nullptr) {
                    return method;
                }
            }

            return nullptr;
        }

    } // namespace heap
} // namespace jvm